<?php 

namespace Admin\Controller;
use Think\Controller;
use Lib\Selfclass;
class CategoryController extends ACommonController {
    public function index(){
    	$catedb = D('Category')->findall();
    	$this->assign('catedb',$catedb);
    	//树类无限分类处理
    	$tree = new \Org\Util\Data();
    	$tree = $tree->tree($catedb);


   	$this->display();
    }
    public function insertCate(){
    		if(IS_POST){
    			$data = array(
    				'cid'=>$_POST['cid'],
    				'cname' => $_POST['cname'],
    				'pid'=>$_POST['pid'],
    				'ckeywords'=>$_POST['ckeywords'],
    				'is_nav'=>$_POST['is_nav'],
    				'ctitle'=>$_POST['ctitle'],
    				'csort'=>$_POST['csort'],
    				'htmldir'=>$_POST['htmldir'],
    				'is_listhtml'=>$_POST['islisthtml'],
    				'is_archtml'=>$_POST['isarchtml'],
    				'is_show'=>$_POST['isshow'],


    				);

    			if(D('Category')->insert($data)){
    					$this->success(returnMsg('1,add,'),U('Category/insertCate'));
    			}else{
    				$this->error(returnMsg('0,add'));
    			}
    		}
    		// 所有顶级分类
    			 $this->assign('Catedb',D('Category')->where('pid=0')->findall());

    	$this->display();
    }
    public function editCate(){
        //查出当前页面的相关信息
        $thiscate = M('category')->where('cid='.I('cid'))->find();
        //show($thiscate);
        $this->assign('thiscate',$thiscate);
         //查出所有的分类 
         $cnamedb = M('category')->field('cname','cid')->select();
         $this->assign('cnamedb',$cnamedb);

         // show($cnamedb);
        $this->display();
    }
    public function delCate(){
        $result = M('category')->where('cid='.I('cid'))->delete();
        if($result!=="false"){
            $this->success('删除成功',U('Category/index'));

        }else{
            $this->error('删除出错');
        }
    }
    /**
     * [creatPingying 生成拼音]
     * @return [type] [description]
     */
    public function AjaxCreatPingying(){
    	if(IS_Ajax){
    		$title = $_POST['title'];
            //树类无限分类处理
    		$ping = new \Org\Util\Stringhd();
    		$titleping = $ping->pinyin($title);
    		return $titleping;
    	}

    }


}